module Dict (Dict,
	     Def,
	     Word, 
	     dict_empty,
	     dict_add,
	     dict_exist,	 		
	     dict_search,	
	     dict_length,
	     dict_del,
	     dict_toDefs
	    )

where


import ListAssoc
import Key
import Data


data Dict = Dict Int (ListAssoc Key Data) deriving Show



type Word = String 
type Def = String


dict_empty :: Dict
dict_empty = Dict 0 la_empty

dict_add :: Dict -> Word -> Def -> Dict
dict_add (Dict n lstAsc) wrd def  | dict_exist (Dict n lstAsc) wrd = Dict n lstAsc
				  | otherwise = Dict (n+1) ((la_add lstAsc (key_fromString wrd) (data_fromString def) ))

dict_exist :: Dict -> Word -> Bool
dict_exist (Dict n xs)  wrd  =  case (la_search xs (key_fromString wrd)) of
				Nothing -> False	
			 	Just _ -> True

dict_search :: Dict -> Word -> Maybe Def
dict_search (Dict n lstAsc ) wrd = case  (la_search  lstAsc (key_fromString wrd)) of
				Nothing -> Nothing
				Just s  ->  Just (data_toString s)

dict_del :: Dict -> Word -> Dict
dict_del (Dict n lstAsc) wrd = case (la_search  lstAsc (key_fromString wrd)) of
		 		Nothing -> Dict n lstAsc
				Just _ -> Dict (n-1) (la_del lstAsc (key_fromString wrd))


dict_length :: Dict -> Int 
dict_length (Dict n lstAsc) = n

dict_toDefs :: Dict -> [(Word,Def)]
dict_toDefs (Dict n lstAsc) = cualquiera (la_toListPair lstAsc)

cualquiera :: [(Key,Data)] -> [(Word,Def)]
cualquiera [] = []
cualquiera ((a,b):xs) = (key_toString a,data_toString b) : cualquiera xs